﻿using System.Web.Mvc;
using Shell.DomainDot;
using Framework.Poco;
using Framework.Domain;
using System.Linq;
using System;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 经销商联系人控制器
    /// </summary>
    public class AgencyContactController : Controller
    {
        private IDomainDotFactory factory;

        /// <summary>
        /// 构造器
        /// </summary>
        public AgencyContactController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 创建经销商的联系人
        /// </summary>
        [HttpGet]
        public ActionResult Create(Agency currentAgency)
        {
            var model = new AgencyContactCreateViewModel();
            var context = factory.Create<AgencyContext>(currentAgency.Id);
            var contact = context.GetAllContact().SingleOrDefault(c => c.IsDefault == false);
            model.Instance = contact;
            return View(model);
        }

        /// <summary>
        /// 创建经销商的联系人
        /// </summary>
        [HttpPost]
        public ActionResult Create(Agency currentAgency, AgencyContactCreateViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyContext>(currentAgency.Id);
                context.AddContact(model.Instance);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改经销商的联系人
        /// </summary>
        [HttpGet]
        public ActionResult Edit(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyContext>(currentAgency.Id);
                var contact = context.GetAllContact().SingleOrDefault(c => c.Id == identity);
                var model = new AgencyContactEditViewModel();
                model.Instance = contact;
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改经销商的联系人
        /// </summary>
        [HttpPost]
        public ActionResult Edit(Agency currentAgency, AgencyContactEditViewModel model)
        {
            if (currentAgency != null)
            {
                if (!String.IsNullOrEmpty(model.Instance.Name))
                {
                    var context = factory.Create<AgencyContext>(currentAgency.Id);
                    context.EditContact(model.Instance);
                    return View(model);
                }
                this.ModelState.AddModelError("Instance.Name", "用户名不能为空!");
                return View();
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 删除经销商的联系人
        /// </summary>
        [HttpGet]
        public ActionResult Delete(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyContext>(currentAgency.Id);
                var model = new AgencyContactDeleteViewModel();
                model.Instance = context.GetAllContact().SingleOrDefault(c => c.Id == identity);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 删除经销商的联系人
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Agency currentAgency, AgencyContactDeleteViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyContext>(currentAgency.Id);
                context.DeleteContact(model.Instance.Id);
                return this.RedirectToAction("Index", "Agency");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }
    }
}